package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.BuyRecord;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.mapper.BuyRecordMapper;
import cn.wolfcode.crm.qo.BuyRecordQueryObject;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IBuyRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BuyRecordService implements IBuyRecordService {
    @Autowired
    private BuyRecordMapper buyRecordMapper;
    @Autowired
    private CustomerService customerService;
    @Override
    public void saveOrUpdate(BuyRecord buyRecord) {
        if(buyRecord.getId() != null){
            buyRecordMapper.updateByPrimaryKey(buyRecord);
        } else {
            buyRecord.setInputTime(new Date());
            //System.out.println(buyRecord.toString());
            customerService.updateStatus(Long.parseLong(buyRecord.getCustomer().getName()), Customer.STATUS_FOMAL);
            buyRecordMapper.insert(buyRecord);
        }
    }


    @Override
    public BuyRecord getBuyRecordById(Long id) {
        return buyRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BuyRecord> getAll() {
        return buyRecordMapper.selectAll();
    }

    @Override
    public PageInfo<BuyRecord> selectByQuery(BuyRecordQueryObject queryObject) {
//        int totalCount = buyRecordMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<BuyRecord> buyRecords = buyRecordMapper.selectByQuery(queryObject);
        return new PageInfo<>(buyRecords);
    }
}
